package com.mallcloud.mall.admin.controller;

import com.mallcloud.mall.admin.api.entity.SysDept;
import com.mallcloud.mall.admin.service.ConnectService;
import com.mallcloud.mall.admin.service.SysDeptService;
import com.mallcloud.mall.common.core.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "开放互联-钉钉、微信、互联")
@RestController
@RequiredArgsConstructor
@RequestMapping("/connect")
public class ConnectController {
	private final ConnectService connectService;
	private final SysDeptService deptService;

	@ApiOperation("同步钉钉用户")
	@PostMapping("/sync/ding/user")
	@PreAuthorize("@pms.hasPermission('sys_connect_sync')")
	public R syncUser() {
		for (SysDept sysDept : deptService.list()) {
			connectService.syncDingUser(sysDept.getDeptId());
		}
		return R.ok();
	}

	@ApiOperation("同步钉钉部门")
	@PostMapping("/sync/ding/dept")
	@PreAuthorize("@pms.hasPermission('sys_connect_sync')")
	public R syncDept() {
		return R.ok(connectService.syncDingDept());
	}

	@ApiOperation("同步企微用户")
	@PostMapping("/sync/cp/user")
	@PreAuthorize("@pms.hasPermission('sys_connect_sync')")
	public R syncCpUser() {
		return connectService.syncCpUser();
	}

	@ApiOperation("同步企微部门")
	@PostMapping("/sync/cp/dept")
	@PreAuthorize("@pms.hasPermission('sys_connect_sync')")
	public R syncCpDept() {
		return connectService.syncCpDept();
	}

}
